package avicit.bdp.dds.common.utils;

import avicit.bdp.common.utils.DateUtils;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dds.common.Constants;
import avicit.bdp.dds.dispatch.model.Server;
import org.apache.commons.lang3.StringUtils;

/**
 * heartbeat for ZK reigster res info
 */
public class ResInfo {

    /**
     * cpuUsage
     */
    private double cpuUsage;

    /**
     * memoryUsage
     */
    private double memoryUsage;

    /**
     * loadAverage
     */
    private double loadAverage;

    private double diskAvailable;

    public ResInfo() {
    }

    public ResInfo(double cpuUsage, double memoryUsage) {
        this.cpuUsage = cpuUsage;
        this.memoryUsage = memoryUsage;
    }

    public ResInfo(double cpuUsage, double memoryUsage, double loadAverage, double diskAvailable) {
        this(cpuUsage, memoryUsage);
        this.loadAverage = loadAverage;
        this.diskAvailable = diskAvailable;
    }

    public double getCpuUsage() {
        return cpuUsage;
    }

    public void setCpuUsage(double cpuUsage) {
        this.cpuUsage = cpuUsage;
    }

    public double getMemoryUsage() {
        return memoryUsage;
    }

    public void setMemoryUsage(double memoryUsage) {
        this.memoryUsage = memoryUsage;
    }

    public double getLoadAverage() {
        return loadAverage;
    }

    public void setLoadAverage(double loadAverage) {
        this.loadAverage = loadAverage;
    }

    /**
     * get CPU and memory usage
     *
     * @param cpuUsage    cpu usage
     * @param memoryUsage memory usage
     * @param loadAverage load average
     * @return cpu and memory usage
     */
    public static String getResInfoJson(double cpuUsage, double memoryUsage, double loadAverage, double diskAvailable) {
        ResInfo resInfo = new ResInfo(cpuUsage, memoryUsage, loadAverage, diskAvailable);
        return JSONUtils.toJson(resInfo);
    }


    /**
     * parse heartbeat info for zk
     *
     * @param heartBeatInfo heartbeat info
     * @return heartbeat info to Server
     */
    public static Server parseHeartbeatForZKInfo(String heartBeatInfo) {
        if (StringUtils.isEmpty(heartBeatInfo)) {
            return null;
        }
        String[] masterArray = heartBeatInfo.split(Constants.COMMA);
        if (masterArray.length != Constants.HEARTBEAT_FOR_ZOOKEEPER_INFO_LENGTH) {
            return null;

        }
        Server masterServer = new Server();
        masterServer.setResInfo(getResInfoJson(Double.parseDouble(masterArray[0]),
                Double.parseDouble(masterArray[1]),
                Double.parseDouble(masterArray[2]), Double.parseDouble(masterArray[10])));
        masterServer.setCreateTime(DateUtils.stringToDate(masterArray[6]));
        masterServer.setLastHeartbeatTime(DateUtils.stringToDate(masterArray[7]));
        //set process id
        masterServer.setId(Integer.parseInt(masterArray[9]));
        return masterServer;
    }

    public double getDiskAvailable() {
        return diskAvailable;
    }

    public void setDiskAvailable(double diskAvailable) {
        this.diskAvailable = diskAvailable;
    }
}
